package com.example.baiduai.dao.impl;

import com.example.baiduai.dao.GenericDao;

import com.example.baiduai.entity.JsonGenericBean;
import com.example.baiduai.tools.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * @author wangyizhu
 * @version "1.8.0_131"
 * @email 2641956765@qq.com
 * @desc:
 * @Time 2022/09/27 18:11
 */
public class GenericDaoImpl implements GenericDao {

    QueryRunner queryRunner = new QueryRunner(DbUtils.getDataSource());
    @Override
    public JsonGenericBean select(String name,String text) {
        // 待执行的sql语句
        String sql = "SELECT * FROM generic where `name`=? and `text`=? ";
        try {
            JsonGenericBean list = queryRunner.query(sql,new BeanHandler<>(JsonGenericBean.class),name,text);
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int insert(String name,String text, String json) {
        String sql = "insert into generic (name,text,json) values(?,?,?)";
        try {
            int update = queryRunner.update(sql, new Object[]{name,text,json});
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
